#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project ：prod_ctrl_service 
@File    ：fluid_injection_handler.py
@IDE     ：PyCharm 
@Author  ：孔令伟
@Date    ：8/23/24 11:20 AM 
"""

from mg_app_framework import HttpBasicHandler, MesCode
from common.log_decorator import log_decorator
from common.params_check_decorator import params_check_decorator
from prod_ctrl_service.service.other_part.fill_electrolyte import fill_electrolyte_check
from prod_ctrl_service.service.preproduction.input_raw import input_raw_check
from prod_ctrl_service.service.production_completed.input_return import input_return_check
from prod_ctrl_service.service.production_process.input_vehicle_cell import check_input_vehicle_cell
from prod_ctrl_service.service.production_process.input_vehicle_instation import check_input_vehicle_instation
from prod_ctrl_service.service.production_process.input_wip_instation import \
    input_wip_instation_check
from prod_ctrl_service.service.production_process.output_singleroll import check_output_singkleroll
from prod_ctrl_service.service.production_process.output_vehicle_cell import check_output_vehicle_cell
from prod_ctrl_service.service.production_process.output_vehicle_whole import check_output_vehicle_whole
from prod_ctrl_service.service.production_process.output_vehicle_wip import check_output_vehicle_wip
from prod_ctrl_service.service.production_process.output_wip import output_wip_check
from prod_ctrl_service.service.production_process.output_wip_newsn import output_wip_newsn_check
from prod_ctrl_service.service.production_process.output_wip_usesn import output_wip_usen_check
from prod_ctrl_service.service.production_process.report_equip_param import \
    report_equip_param_check
from prod_ctrl_service.service.production_process.report_prod_param import report_prod_param_check
from prod_ctrl_service.service.production_process.wip_check import wip_check
from prod_ctrl_service.service.production_process.wip_check_out import check_wip_check_out
from prod_ctrl_service.service.production_process.wip_no_check import wip_no_check
from prod_ctrl_service.tool.tool import check_basic_param


class InputWipInstationHandler(HttpBasicHandler):
    @log_decorator()
    @params_check_decorator(check_params=["trace_id", "call_id", "upperpc_code", "process_code"])
    async def post_process(self, *args, **kwargs):
        params: dict = self.data
        result = await input_wip_instation_check(params)
        self.send_response_data(result["code"], result["data"], result["info"])


class ReportProdParamHandler(HttpBasicHandler):
    @log_decorator()
    @params_check_decorator(check_params=["trace_id", "call_id", "upperpc_code", "process_code"])
    async def post_process(self, *args, **kwargs):
        params: dict = self.data
        result = await report_prod_param_check(params=params)
        self.send_response_data(result["code"], result["data"], result["info"])


class ReportEquipParamHandler(HttpBasicHandler):
    @log_decorator()
    @params_check_decorator(check_params=["trace_id", "call_id", "upperpc_code", "process_code"])
    async def post_process(self, *args, **kwargs):
        params: dict = self.data
        result = await report_equip_param_check(params=params)
        self.send_response_data(result["code"], result["data"], result["info"])


class OutputWipHandler(HttpBasicHandler):
    @log_decorator()
    @params_check_decorator(check_params=["trace_id", "call_id", "upperpc_code", "process_code"])
    async def post_process(self, *args, **kwargs):
        params: dict = self.data
        result = await output_wip_check(params=params)
        self.send_response_data(result["code"], result["data"], result["info"])


class InputReturnHandler(HttpBasicHandler):
    @log_decorator()
    @params_check_decorator(check_params=["trace_id", "call_id", "upperpc_code", "process_code"])
    async def post_process(self, *args, **kwargs):
        params: dict = self.data
        result = await input_return_check(params)
        self.send_response_data(result["code"], result["data"], result["info"])


class InputRawHandler(HttpBasicHandler):
    @log_decorator()
    @params_check_decorator(check_params=["trace_id", "call_id", "upperpc_code", "process_code"])
    async def post_process(self, *args, **kwargs):
        params: dict = self.data
        result = await input_raw_check(params=params)
        self.send_response_data(result["code"], result["data"], result["info"])


class FileElectrolyteHandler(HttpBasicHandler):
    @log_decorator()
    @params_check_decorator(check_params=["trace_id", "call_id", "upperpc_code", "process_code"])
    async def post_process(self, *args, **kwargs):
        params: dict = self.data
        result = await fill_electrolyte_check(params=params)
        self.send_response_data(result["code"], result["data"], result["info"])


class OutputWipUsenHandler(HttpBasicHandler):
    @log_decorator()
    @params_check_decorator(check_params=["trace_id", "call_id", "upperpc_code", "process_code"])
    async def post_process(self, *args, **kwargs):
        params: dict = self.data
        result = await output_wip_usen_check(params=params)
        self.send_response_data(result["code"], result["data"], result["info"])


class WipNoCheckHandler(HttpBasicHandler):
    @log_decorator()
    async def post_process(self, *args, **kwargs):
        params: dict = self.data
        result = await wip_no_check(params=params)
        self.send_response_data(result["code"], result["data"], result["info"])


class OutputWipNewsnHandler(HttpBasicHandler):
    @log_decorator()
    @params_check_decorator(check_params=["trace_id", "call_id", "upperpc_code", "process_code"])
    async def post_process(self, *args, **kwargs):
        params: dict = self.data
        result = await output_wip_newsn_check(params=params)
        self.send_response_data(result["code"], result["data"], result["info"])


class WipCheckHandler(HttpBasicHandler):
    @log_decorator()
    @params_check_decorator(check_params=["trace_id", "call_id", "upperpc_code", "process_code"])
    async def post_process(self, *args, **kwargs):
        params: dict = self.data
        result = await wip_check(params)
        self.send_response_data(result["code"], result["data"], result["info"])


class InputVehicleInstation(HttpBasicHandler):
    @log_decorator()
    @params_check_decorator(check_params=["trace_id", "call_id", "upperpc_code", "process_code"])
    async def post_process(self, *args, **kwargs):
        params: dict = self.data
        param_check_msg = ""
        # '接口调用方认证ID/接口调用ID/上位机编码/员工编码/设备编码'是每一个接口都有且必填的入参，检查这些参数是否填写
        basic_param_check_result, basic_param_check_message = check_basic_param(params=params)
        if not basic_param_check_result:
            param_check_msg += basic_param_check_message
        vehicle_no = params.get("vehicle_no", "")
        if not vehicle_no:
            param_check_msg += f"请检查‘载具编码(vehicle_no)’是否填写;"
        cell_list = params.get("cell_list", [])
        if not cell_list:
            param_check_msg += f"请检查‘电芯列表(cell_list)’是否填写;"
        else:
            for index, cell_unit in enumerate(cell_list):
                cell_sn = cell_unit.get("cell_sn")
                if not cell_sn:
                    param_check_msg += f"电芯列表中第{index + 1}个电芯码未填写;"
        if param_check_msg:
            self.send_response_data(MesCode.fail, {}, param_check_msg)
        else:
            result = await check_input_vehicle_instation(params)
            self.send_response_data(result["code"], result["data"], result["info"])


class OuputVehicleWipHandler(HttpBasicHandler):
    @log_decorator()
    @params_check_decorator(check_params=["trace_id", "call_id", "upperpc_code", "process_code"])
    async def post_process(self, *args, **kwargs):
        params: dict = self.data
        param_check_msg = ""
        # '接口调用方认证ID/接口调用ID/上位机编码/员工编码/设备编码'是每一个接口都有且必填的入参，检查这些参数是否填写
        basic_param_check_result, basic_param_check_message = check_basic_param(params=params)
        if not basic_param_check_result:
            param_check_msg += basic_param_check_message
        vehicle_no = params.get("vehicle_no", "")
        if not vehicle_no:
            param_check_msg += f"请检查‘托盘码(vehicle_no)’是否填写;"
        cell_list = params.get("cell_list", [])
        if not cell_list:
            param_check_msg += f"请检查‘托盘内电芯列表(cell_list)’是否填写;"
        is_ng = params.get("is_ng", None)
        if is_ng not in [0, 1]:
            param_check_msg += f"请检查‘是否NG品(is_ng)’是否填写;"
        # param_list = params.get("param_list", None)
        # if param_list:
        #     for index, param_unit in enumerate(param_list):
        #         param_code = param_unit.get("param_code", None)
        #         param_value = param_unit.get("param_value", None)
        #         if param_code is None or param_value is None:
        #             param_check_msg += f"结果参数列表中第{index + 1}个结果参数未填写完整;"
        # else:
        #     param_check_msg += f"结果参数列表(param_list)未填写;"
        if param_check_msg:
            self.send_response_data(MesCode.fail, {}, param_check_msg)
        else:
            result = await check_output_vehicle_wip(params)
            self.send_response_data(result["code"], result["data"], result["info"])


class InputVehicleCellHandler(HttpBasicHandler):
    @log_decorator()
    @params_check_decorator(check_params=["trace_id", "call_id", "upperpc_code", "process_code"])
    async def post_process(self, *args, **kwargs):
        params: dict = self.data
        param_check_msg = ""
        # '接口调用方认证ID/接口调用ID/上位机编码/员工编码/设备编码'是每一个接口都有且必填的入参，检查这些参数是否填写
        basic_param_check_result, basic_param_check_message = check_basic_param(params=params)
        if not basic_param_check_result:
            param_check_msg += basic_param_check_message
        vehicle_no = params.get("vehicle_no", "")
        if not vehicle_no:
            param_check_msg += f"请检查‘载具编码(vehicle_no)’是否填写;"
        cell_list = params.get("cell_list", [])
        if not cell_list:
            param_check_msg += f"请检查‘电芯列表(cell_list)’是否填写;"
        else:
            for index, cell_unit in enumerate(cell_list):
                cell_sn = cell_unit.get("cell_sn")
                if not cell_sn:
                    param_check_msg += f"电芯列表中第{index + 1}个电芯码未填写;"
        if param_check_msg:
            self.send_response_data(MesCode.fail, {}, param_check_msg)
        else:
            result = await check_input_vehicle_cell(params)
            self.send_response_data(result["code"], result["data"], result["info"])


class OuputVehicleCellHandler(HttpBasicHandler):
    @log_decorator()
    @params_check_decorator(check_params=["trace_id", "call_id", "upperpc_code", "process_code"])
    async def post_process(self, *args, **kwargs):
        params: dict = self.data
        param_check_msg = ""
        # '接口调用方认证ID/接口调用ID/上位机编码/员工编码/设备编码'是每一个接口都有且必填的入参，检查这些参数是否填写
        basic_param_check_result, basic_param_check_message = check_basic_param(params=params)
        if not basic_param_check_result:
            param_check_msg += basic_param_check_message
        vehicle_no = params.get("vehicle_no", "")
        if not vehicle_no:
            param_check_msg += f"请检查‘托盘码(vehicle_no)’是否填写;"
        input_time = params.get("input_time", "")
        if not input_time:
            param_check_msg += f"请检查‘进站时间(input_time)’是否填写;"
        output_time = params.get("output_time", "")
        if not output_time:
            param_check_msg += f"请检查‘出站时间(output_time)’是否填写;"
        cell_list = params.get("cell_list", [])
        if not cell_list:
            param_check_msg += f"请检查‘托盘内电芯列表(cell_list)’是否填写;"
        for index, cell_unit in enumerate(cell_list):
            cell_sn = cell_unit.get("cell_sn")
            if not cell_sn:
                param_check_msg += f"电芯列表中第{index + 1}个电芯码未填写;"
            slot_no = cell_unit.get("slot_no")
            if not slot_no:
                param_check_msg += f"电芯列表中第{index + 1}个通道号未填写;"
            is_ng = cell_unit.get("is_ng", None)
            if is_ng not in [0, 1]:
                param_check_msg += f"电芯列表中第{index + 1}个是否NG品未填写;"
            # param_list = cell_unit.get("param_list", None)
            # if param_list:
            #     for sub_index, param_unit in enumerate(param_list):
            #         param_code = param_unit.get("param_code", None)
            #         param_value = param_unit.get("param_value", None)
            #         if param_code is None or param_value is None:
            #             param_check_msg += f"电芯列表中第{index + 1}个结果参数列表中第{sub_index + 1}个结果参数未填写完整;"
            # else:
            #     param_check_msg += f"结果参数列表(param_list)未填写;"
        if param_check_msg:
            self.send_response_data(MesCode.fail, {}, param_check_msg)
        else:
            result = await check_output_vehicle_cell(params)
            self.send_response_data(result["code"], result["data"], result["info"])


class WipCheckOutHandler(HttpBasicHandler):
    @log_decorator()
    @params_check_decorator(check_params=["trace_id", "call_id", "upperpc_code", "process_code"])
    async def post_process(self, *args, **kwargs):
        params: dict = self.data
        param_check_msg = ""
        # '接口调用方认证ID/接口调用ID/上位机编码/员工编码/设备编码'是每一个接口都有且必填的入参，检查这些参数是否填写
        basic_param_check_result, basic_param_check_message = check_basic_param(params=params)
        if not basic_param_check_result:
            param_check_msg += basic_param_check_message
        cell_list: list = params.get("cell_list", [])
        if not cell_list:
            param_check_msg += f"请检查‘电芯码列表(cell_list)’是否填写;"
        if param_check_msg:
            self.send_response_data(MesCode.fail, {}, param_check_msg)
        else:
            result = await check_wip_check_out(params)
            self.send_response_data(result["code"], result["data"], result["info"])


class OutputSinglerollHandler(HttpBasicHandler):
    @log_decorator()
    @params_check_decorator(check_params=["trace_id", "call_id", "upperpc_code", "process_code"])
    async def post_process(self, *args, **kwargs):
        params: dict = self.data
        param_check_msg = ""
        # '接口调用方认证ID/接口调用ID/上位机编码/员工编码/设备编码'是每一个接口都有且必填的入参，检查这些参数是否填写
        basic_param_check_result, basic_param_check_message = check_basic_param(params=params)
        if not basic_param_check_result:
            param_check_msg += basic_param_check_message
        src_wip_type = params.get("src_wip_type", None)
        src_wip_no = params.get("src_wip_no", None)
        src_up_septum = params.get("src_up_septum", None)
        src_down_septum = params.get("src_down_septum", None)
        src_tape = params.get("src_tape", None)
        singleroll_cnt = params.get("singleroll_cnt", None)
        check_list = [
            src_wip_type not in [0, 1], src_wip_type is None,
            not src_wip_no, src_wip_no is None,
            not src_down_septum, src_down_septum is None,
            not src_tape, src_tape is None,
            not src_up_septum, src_up_septum is None,
            singleroll_cnt not in [0, 1], singleroll_cnt is None,
        ]
        if any(check_list):
            param_check_msg += f"请检查‘正负极卷料类型(src_wip_type)/正负极卷料码(src_wip_no)/上隔膜原料码(src_up_septum)/" \
                               f"下隔膜原料码(src_down_septum)/胶带原料码(src_tape)/单卷数量(singleroll_cnt)’是否填写;"
        if param_check_msg:
            self.send_response_data(MesCode.fail, {}, param_check_msg)
        else:
            result = await check_output_singkleroll(params)
            self.send_response_data(result["code"], result["data"], result["info"])


class OuputVehicleWholeHandler(HttpBasicHandler):
    @log_decorator()
    @params_check_decorator(check_params=["trace_id", "call_id", "upperpc_code", "process_code"])
    async def post_process(self, *args, **kwargs):
        params: dict = self.data
        param_check_msg = ""
        # '接口调用方认证ID/接口调用ID/上位机编码/员工编码/设备编码'是每一个接口都有且必填的入参，检查这些参数是否填写
        basic_param_check_result, basic_param_check_message = check_basic_param(params=params)
        if not basic_param_check_result:
            param_check_msg += basic_param_check_message
        vehicle_no = params.get("vehicle_no", "")
        if not vehicle_no:
            param_check_msg += f"请检查‘托盘码(vehicle_no)’是否填写;"
        input_time = params.get("input_time", "")
        if not input_time:
            param_check_msg += f"请检查‘进站时间(input_time)’是否填写;"
        output_time = params.get("output_time", "")
        if not output_time:
            param_check_msg += f"请检查‘出站时间(output_time)’是否填写;"
        cell_list = params.get("cell_list", [])
        if not cell_list:
            param_check_msg += f"请检查‘托盘内电芯列表(cell_list)’是否填写;"
        for index, cell_unit in enumerate(cell_list):
            cell_sn = cell_unit.get("cell_sn")
            if not cell_sn:
                param_check_msg += f"电芯列表中第{index + 1}个电芯码未填写;"
            slot_no = cell_unit.get("slot_no")
            if not slot_no:
                param_check_msg += f"电芯列表中第{index + 1}个通道号未填写;"
        is_ng = params.get("is_ng", None)
        if is_ng not in [0, 1]:
            param_check_msg += f"请检查‘是否NG品(is_ng)’是否填写;"
        # param_list = params.get("param_list", None)
        # if param_list:
        #     for sub_index, param_unit in enumerate(param_list):
        #         param_code = param_unit.get("param_code", None)
        #         param_value = param_unit.get("param_value", None)
        #         if any([param_code is None, param_value is None, not param_code, not param_value]):
        #             param_check_msg += f"结果参数列表中第{sub_index + 1}个结果参数未填写完整;"
        # else:
        #     param_check_msg += f"结果参数列表(param_list)未填写;"
        if param_check_msg:
            self.send_response_data(MesCode.fail, {}, param_check_msg)
        else:
            result = await check_output_vehicle_whole(params)
            self.send_response_data(result["code"], result["data"], result["info"])
